package com.lagou.blog.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lagou.blog.entity.Article;
import com.lagou.blog.entity.BaseQuery;
import com.lagou.blog.entity.BlogPage;
import com.lagou.blog.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class BlogController {

    @Autowired
    private BlogService blogService;

    @RequestMapping({"/", "/index"})
    public String index(Model model, Page<Article> page,
            @RequestParam(defaultValue = "3", required = false) int pageSize, Article article) {
        BaseQuery<Article> query = new BaseQuery<>();
        page.setSize(pageSize);
        if (page.getCurrent() < 1) {
            page.setCurrent(1);
        }
        query.setPage(page);
        IPage<Article> r = this.blogService.selectPage(query);
        page.setTotal(r.getTotal());
        page.setRecords(r.getRecords());
        model.addAttribute("page", page);

        return "/client/index";
    }

    @RequestMapping("/page")
    @ResponseBody
    public Object page(BlogPage<Article> page, Article article) {
        BaseQuery<Article> query = new BaseQuery<>();
        query.setPage(page);
        return this.blogService.selectPage(query);
    }
}
